package com.fpd.tools.redis;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;


public class RedisConsumer {

    @Autowired
    private RedisTemplate redisTemplate;

    @PostConstruct
    public void init(){
        dataToDb();
    }

    /**
     * 用户导入
     */
    private void dataToDb() {
        System.out.println("====redis运行====");
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(()->{
            while(true) {
                Object obj = redisTemplate.opsForList().rightPop("importFileList", 1, TimeUnit.SECONDS);
                if (obj == null) {
                    //没有数据，5秒钟轮询一次
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    continue;
                }
                JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(obj));
            }
        });
    }
}
